package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.CourseUnitSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.service.CourseUnitServiceAsync;
import com.rattsoftware.client.service.UserServiceAsync;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.CourseUnitView;
import com.rattsoftware.server.admin.model.CourseUnit;
import com.rattsoftware.server.admin.model.Editable;
import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.exceptions.RattsParsingException;

public class CourseUnitController extends Controller {

	AppMessages appMessages = GWT.create(AppMessages.class);

	private CourseUnitView courseUnitView;
	private UserServiceAsync userService;
	private CourseUnitServiceAsync courseUnitService;

	public CourseUnitController() {
		super();
		registerEventTypes(AppEvents.ViewCourseUnits);
	}

	@Override
	protected void initialize() {
		GWT.log("CourseUnitController : initialize");
		courseUnitView = new CourseUnitView(this);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_COURSE_UNIT, courseUnitView);
		courseUnitService = Registry.get(webclient.COURSE_UNIT_SERVICE);
	}

	@Override
	public void handleEvent(AppEvent event) {
		GWT.log("CourseUnitController : handleEvent");
		EventType type = event.getType();
		if (type == AppEvents.ViewCourseUnits) {
			forwardToView(courseUnitView, event);
		}
	}

	/*public void getUser() {
		userService = Registry.get(webclient.USER_SERVICE);
		userService.getLoggedInUser(new RattsAsyncCallback<User>() {
			@Override
			public void onSuccess(User result) {
				courseUnitView.setUser(result);
			}
		});
	}*/
	
	public void getCoursesUnits(final PagingLoadConfig pagingLoadConfig, 
			final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<CourseUnitSummary>> callback)
			throws RattsParsingException {
		GWT.log("CourseUnitController : getCourseUnits");
		User user = Registry.get(webclient.LOGGED_IN_USER_NAME);
		
		/*courseService.getCourseList(user, PagingUtil.createPagingLoadConfig(pagingLoadConfig),
				new RattsAsyncCallback<PagingLoadResult<Course>>() {
					@Override
					public void onSuccess(PagingLoadResult<Course> result) {*/
						ProgressIndicator.hideProgressBar();
						List<CourseUnitSummary> results = new ArrayList<CourseUnitSummary>();
						//List<Course> data = result.getData();
						List<CourseUnit> data = TestData.getCourseUnits();
						courseUnitView.setAllCourseUnitSummaries(results); // reset the list
						for (CourseUnit f : data) {
							results.add(new CourseUnitSummary(f));
						}
						callback.onSuccess(new BasePagingLoadResult<CourseUnitSummary>(results, 0, 20));
						/*callback.onSuccess(new BasePagingLoadResult<CourseSummary>(results, result.getOffset(), result
								.getTotalLength()));
					}
				});*/
	}
	
	public void deleteCourseUnit(final Editable editable) {
		if (editable instanceof CourseUnit) {

			GWT.log("CourseUnitController : delete Course Unit");
			/*courseUnitService.deleteCourseUnit((CourseUnit)editable, new RattsAsyncCallback<Void>() {
				@Override
				public void onSuccess(Void result) {*/
					RefreshablePublisher.get().publish(new RefreshableEvent(RefreshableEvent.Type.DELETE_COURSE_UNIT, editable));
				/*}
			});*/
		}
	}
	

	public void forwardToNewEditCourseUnitController(CourseUnitSummary courseUnitSummary) {
		GWT.log("CourseUnitController : forwardToNewEditCourseUnitController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.EditCourseUnit, courseUnitSummary);
	}
}
